package co.yixiang.modules.shop.service.impl;


import co.yixiang.modules.shop.domain.YxStoreBrand;
import co.yixiang.modules.shop.domain.YxStorePlace;
import co.yixiang.modules.shop.repository.YxStorePlaceRepository;
import co.yixiang.modules.shop.service.YxStorePlaceService;
import co.yixiang.modules.shop.service.dto.YxStoreBrandDto;
import co.yixiang.modules.shop.service.dto.YxStorePlaceDTO;
import co.yixiang.modules.shop.service.dto.YxStorePlaceQueryCriteria;
import co.yixiang.modules.shop.service.mapper.YxStorePlaceMapper;
import co.yixiang.utils.FileUtil;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import co.yixiang.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author frq
 * @date 2020-07-10
 */

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStorePlaceServiceImpl implements YxStorePlaceService {

    @Autowired
    private YxStorePlaceRepository yxStorePlaceRepository;

    @Autowired
    private YxStorePlaceMapper yxStorePlaceMapper;

    /**
     * 带分页查询
     * @param criteria 条件
     * @param pageable 分页参数
     * @return
     */
    @Override
    public Map<String, Object> queryAll(YxStorePlaceQueryCriteria criteria, Pageable pageable) {
        Page<YxStorePlace> page = yxStorePlaceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxStorePlaceMapper::toDto));
    }

    /**
     * 查询所有数据不分页
     * @param criteria 条件参数
     * @return
     */
    @Override
    public List<YxStorePlaceDTO> queryAll(YxStorePlaceQueryCriteria criteria) {
        return yxStorePlaceMapper.toDto(yxStorePlaceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    /**
     * 根据ID查询
     * @param id ID
     * @return
     */
    @Override
    public YxStorePlaceDTO findById(Integer id) {
        YxStorePlace yxStorePlace = yxStorePlaceRepository.findById(id).orElseGet(YxStorePlace::new);
        ValidationUtil.isNull(yxStorePlace.getId(),"YxStoreBrand","id",id);
        return yxStorePlaceMapper.toDto(yxStorePlace);
    }

    /**
     * 创建
     * @param resources /
     * @return
     */
    @Override
    public YxStorePlaceDTO create(YxStorePlace resources) {
        resources.setAddTime(new Date());
        resources.setIsDel(0);
        return yxStorePlaceMapper.toDto(yxStorePlaceRepository.save(resources));
    }

    /**
     * 编辑
     * @param resources /
     */
    @Override
    public void update(YxStorePlace resources) {
        YxStorePlace yxStorePlace = yxStorePlaceRepository.findById(resources.getId()).orElseGet(YxStorePlace::new);
        ValidationUtil.isNull( yxStorePlace.getId(),"YxStoreBrand","id",resources.getId());
        yxStorePlace.copy(resources);
        yxStorePlaceRepository.save(yxStorePlace);
    }

    /**
     * 多选删除
     * @param ids /
     */
    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            yxStorePlaceRepository.deleteById(id);
        }
    }

    /**
     * 导出数据
     * @param all 待导出的数据
     * @param response /
     * @throws IOException
     */
    @Override
    public void download(List<YxStorePlaceDTO> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxStorePlaceDTO yxStorePlaceDTO : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("场地名称", yxStorePlaceDTO.getNationName());
            map.put("排序", yxStorePlaceDTO.getSort());
            map.put("图标", yxStorePlaceDTO.getPic());
            map.put("添加时间", yxStorePlaceDTO.getAddTime());
            map.put("删除状态", yxStorePlaceDTO.getIsDel());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
